package com.itts.hk.utils;

import com.google.common.collect.Lists;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
    @Test
    public void getStatetime() throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -7);//7天前
        Date monday = c.getTime();
        String preMonday = sdf.format(monday);
        System.out.println(preMonday);
    }

    @Test
    public void test12() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -1);// 1分钟之前的时间
        Date beforeD = beforeTime.getTime();

        String format = sdf.format(beforeD);
        System.out.println(format);
        System.out.println(sdf.format(new Date()));
    }

    @Test
    public void test1() {
        //Scanner input = new Scanner(System.in);
        //long n = input.nextLong();
        long n = 4564;
        System.out.println(millisToStringShort(n));
    }

    //秒转化为时分秒
    public static StringBuffer millisToStringShort(long l) {
        StringBuffer sb = new StringBuffer();
        long millis = 1;

        long minutes = 60 * millis;
        long hours = 60 * minutes;
        long days = 24 * hours;

        if (l / days >= 1)
            sb.append((int) (l / days) + "天");
        if (l % days / hours >= 1)
            sb.append((int) (l % days / hours) + "小时");
        if (l % days % hours / minutes >= 1)
            sb.append((int) (l % days % hours / minutes) + "分钟");
        if (l % days % hours % minutes >= 1)
            sb.append((int) (l % days % hours % minutes) + "秒");
        return sb;
    }

    @Test
    /**
     * 实现java 中 list集合中有几十万条数据,每100条为一组取出
     * @param list 可穿入几十万条数据的List
     * @return map 每一Kye中有100条数据的List
     */
    public void groupList() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add(i);
        }
        List<List<Integer>> lists = Lists.partition(list, 20);
        System.out.println(lists);
        System.out.println(lists.size());

    }

    @Test
    public void test3() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = "2021-09-27 13:08:00";
        try {
            Date parse = sdf.parse(date);
            System.out.println(parse.getTime());

            long l = parse.getTime() + 30;
            long a = 1632719280030l;
            StringBuffer stringBuffer = millisToStringShort(a);
            System.out.println(stringBuffer);
            Date date1 = new Date(a);
            System.out.println(sdf.format(date1));
        } catch (ParseException e) {
            e.printStackTrace();
        }


    }
    /**
     * 根据格式获取当前格式化时间
     *
     * @param format 格式化方式，基础格式为yyyy-MM-dd HH:mm:ss
     * @return 当前时间
     */
    public static String getCurrentTimeByFormat(String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(new Date());
    }

    /**
     * 格式化时间
     *
     * @param format 格式化格式，基础格式为yyyy-MM-dd HH:mm:ss
     * @param
     * @return
     */
    public static String formatTime(String format, long time) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(new Date(time));
    }

    @Test
    public void tet1() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = "2021-09-27 13:08:10"; //开始时间

        int a=60;
        Calendar  cd = Calendar.getInstance();
        long l = a * 1000L;
        cd.setTimeInMillis(l);

        Date time = cd.getTime();
        Date parse = sdf.parse(date);

        long lo = parse.getTime() - time.getTime();
        String s = sdf.format(new Date(lo));
        System.out.println(s); //结束时间2021-09-27 13:09:10
    }

    @Test
    public void tet10() {

    }
}
